<template>
  <!-- // 选择图片 -->
  <div>
    <!-- :on-success="handleAvatarSuccess" -->
    <el-upload
      class="avatar-uploader"
      action="https://jsonplaceholder.typicode.com/posts/"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="upload"
    >
      <img v-if="userData" :src="userData" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon" />
      <!-- 进度条 -->
      <!-- type=' circle' -->
      <el-progress
        v-show="showProcess"
        type="line"
        :percentage="percentage"
        status="success"
        class="progress"
      />
    </el-upload>
  </div>
</template>

<script>
// 导入cos的SDK调用
import COS from 'cos-js-sdk-v5'

const cos = new COS({
  SecretId: 'AKIDjETSCffIWVEm47PNgmdDYdT4OVWIq9F8', // 身份识别ID
  SecretKey: 'lHehZDkGjywgy2uY1LSIvISNbR2OTN2S' // 身份秘钥
})
export default {
  name: 'UploadPhoto',
  props: {
    // 图片
    userData: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      imageUrl: '',
      // 进度条的长度：0-100
      percentage: 0,
      // 进度条的显示和隐藏
      showProcess: false
    }
  },
  methods: {
    // 上传成功之后执行的回调函数
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    // 选择完图标之后校验  格式或大小
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    upload (res) {
      console.log(res)
      // 腾讯云SDK上传...
      if (res.file) {
        // 显示
        this.showProcess = true
        // 执行上传操作
        cos.putObject({
          Bucket: 'hr-admin-template-1305861561', /* 存储桶名字 hrsass-1255477649 */
          Region: 'ap-beijing', /* 存储桶所在地域，必须字段 ap-beijing */
          Key: res.file.name, /* 文件名 */
          StorageClass: 'STANDARD', // 上传模式, 标准模式
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            // 上传之后,完成之前会一直执行 , 上传进度条在这里实现
            console.log(JSON.stringify(progressData))
            this.percentage = progressData.percent * 100
          }
        }, (err, data) => {
          console.log(err || data)
          // 上传成功之后
          // err没有值表示上传成功
          if (!err) {
            // 回显上传成功的图片
            // this.imageUrl = `https://${data.Location}`
            this.$emit('update:userData', `https://${data.Location}`)
            this.showProcess = false
            this.percentage = 0
          }
        })
      }
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
